package com.jiuyin.dianjing.db.util.helper;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import com.jiuyin.dianjing.db.table.SearchHistoryColumn;
import com.jiuyin.dianjing.db.util.helper.base.DbProviderHelper;
import com.jiuyin.dianjing.model.SearchHistoryModel;
import com.jiuyin.dianjing.util.LogUtil;

import java.util.ArrayList;
import java.util.List;

import static com.jiuyin.dianjing.constant.HelperConstant.CONTENT_URI_SEARCH_HISTORY;

/**
 * Description: 搜索历史数据库管理
 * File: SearchHistoryProviderHelper.java
 * Create: 2018/09/16 10:44
 *
 * @author Administrator
 */
public class SearchHistoryProviderHelper implements DbProviderHelper<List<SearchHistoryModel>> {
    private static Uri mUri = Uri.parse(CONTENT_URI_SEARCH_HISTORY);
    private static SearchHistoryProviderHelper instance = new SearchHistoryProviderHelper();

    public static SearchHistoryProviderHelper getInstance() {
        return instance;
    }

    @Override
    public List<SearchHistoryModel> getDbInfo(Context context) {
        List<SearchHistoryModel> searchHistoryModelList = new ArrayList<>();
        try (Cursor c = context.getContentResolver().query(mUri,
                null,
                null,
                null,
                "_id DESC")) {
            while (c != null && c.moveToNext()) {
                SearchHistoryModel model = new SearchHistoryModel();
                String content = (c.getString(c.getColumnIndexOrThrow(SearchHistoryColumn.CONTENT)));
                long id = (c.getLong(c.getColumnIndexOrThrow(SearchHistoryColumn._ID)));
                model.setContent(content);
                model.setId(id);
                searchHistoryModelList.add(model);
            }
        } catch (Exception e) {
            LogUtil.log("SearchHistoryProviderHelper selectDbInfo exception = " + e.getMessage());
            return searchHistoryModelList;
        }
        return searchHistoryModelList;
    }

    /**
     * upload result provider data update db
     * update treated
     */
    public synchronized void update(long id, String content, Context context) {
        ContentValues values = new ContentValues();
        Uri updateIdUri = ContentUris.withAppendedId(mUri, id);
        values.put(SearchHistoryColumn.CONTENT, content);
        context.getContentResolver().update(updateIdUri, values, null, null);
    }

    @Override
    public synchronized boolean insertDb(ContentResolver contentResolver, List<SearchHistoryModel> model) {
        if (null == contentResolver || null == model) {
            return false;
        }
        int size = model.size();
        ContentValues[] values = new ContentValues[size];
        for (int i = 0; i < size; i++) {
            ContentValues value = new ContentValues();
            value.put(SearchHistoryColumn.CONTENT, model.get(i).getContent());
            values[i] = value;
        }
        return (0 == contentResolver.bulkInsert(mUri, values));
    }


    /**
     * @param context Context
     */
    public synchronized void deleteDb(Context context) {
        try {
            ContentResolver contentResolver = context.getContentResolver();
            contentResolver.delete(mUri, null, null);
            LogUtil.log("SearchHistoryProviderHelper deleteDb");
        } catch (Exception e) {
            LogUtil.log("SearchHistoryProviderHelper deleteDb exception =  " + e.getMessage());
        }
    }

    /**
     * 从主数据库删除一条订单
     *
     * @param context Context
     * @param id      table id
     */
    public synchronized void deleteOneDb(Context context, long id) {
        try {
            Uri deleteUri = ContentUris.withAppendedId(mUri, id);
            ContentResolver contentResolver = context.getContentResolver();
            contentResolver.delete(deleteUri, null, null);
        } catch (Exception e) {
            LogUtil.log("SearchHistoryProviderHelper deleteOneDb exception = " + e.getMessage());
        }
    }
}
